package rabbitMQ.client;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consume {

    public static void main(String[] args) throws IOException, TimeoutException {

        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.8.102");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");

        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();
//        channel.exchangeDeclare("ltTestExchange", BuiltinExchangeType.DIRECT, true, false, false, null);
//
//        channel.queueDeclare("ltTestQueue", true, false, false, null);
//
//        channel.queueBind("ltTestQueue", "ltTestExchange", "ltTestRoutingKey");
                              while (true) {
                                  channel.basicConsume("ltTestQueue", false, "", new DefaultConsumer(channel) {

                                      @Override
                                      public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                                          super.handleDelivery(consumerTag, envelope, properties, body);
                                          String routingKey = envelope.getRoutingKey();
                                          String contentType = properties.getContentType();
                                          long deliveryTag = envelope.getDeliveryTag();
                                          channel.basicAck(deliveryTag, false);
                                          String s = new String(body);
                                          System.out.println(s);
                                      }

                                      @Override
                                      public void handleConsumeOk(String consumerTag) {
                                          super.handleConsumeOk(consumerTag);
//                                          System.out.println(consumerTag + "----------handleConsumeOk");
                                      }

                                      @Override
                                      public void handleCancelOk(String consumerTag) {
                                          super.handleCancelOk(consumerTag);
//                                          System.out.println(consumerTag + "----------handleCancelOk");

                                      }
                                  });
                              }
//        channel.close();
//        connection.close();
    }

}
